package com.moon.game.command.guest.lair;

import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 召唤珍兽
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-14 下午11:22:14
 */
public class DestoryLairCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "摧毁孵化塔出错，你当前没有孵化塔！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		boolean hasTheLair = false;
		Tower theTower = null;
		/*List<Tower> newTowers = new ArrayList<PlayerVo.Tower>();
		for (Tower tower : towers) {
			if (tower.getTowerId() == towerId.intValue()) {
				tower.setLastShowTime(GDateUtils.now());
				hasTheLair = true;
			}else {
				newTowers.add(tower);
			}
		}*/
		for (Tower tower : towers) {
			if (tower.getTowerId() == towerId.intValue()) {
				tower.setLastShowTime(GDateUtils.now());
				hasTheLair = true;
				theTower = tower;
			}
		}
		//没有指定孵化塔
		if (!hasTheLair) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		
		//是否确认
		Integer sure = Integer.parseInt(ps[1]);
		if (sure == null) {
			doResponsePsError(req);
			return;
		}
		req.setVO("tower_id", towerId);
		boolean hasRenting = false;
		
		//孵化塔正在出租，提示是否继续刷
		if (theTower.getStartRentTime() + theTower.getRentTime() > GDateUtils.now()) {
			hasRenting = true;
		}
		if (sure.intValue() == 1) {
			//孵化塔正在出租，提示是否继续刷
			if (hasRenting) {
				//确定取消
				LairService.cancelTheRentingTower(theTower);
			}
			
			//确认拆除建筑
			boolean minusStatu = PlayerService.minusMoney(req.getSid(), req.getPlayerId(), Constants.DESTORY_LAIR_PRICE);
			if (!minusStatu) {
				//碎银不足
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_MONEY, "摧毁孵化塔需要碎银" + Constants.DESTORY_LAIR_PRICE + ",你的碎银不足！"));
				return;
			}
			towers.remove(theTower);
//			player.setTowerList(newTowers);
			PlayerService.doUpdatePlayer(req.getSid(), req.getPlayerId(), player);
			String info = "摧毁成功，您可以在空地上重新修建孵化塔了！";
			req.setVO(TAG_INFO, info);
		}else {
			//拆除建筑前询问
			req.setVO(TAG_ACK, true);
			req.setVO("has_renting", hasRenting);
		}
		response(req, req.getClientResponse());
	}
}
